package org.itboys.mobile.service.mysql.web.wallet;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.wallet.CooWalletDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.wallet.CooWallet;
import org.itboys.mobile.service.mongo.web.member.SundayMemberService;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/2/1
 * 描述：钱包
 */
@Service
public class CooWalletService extends BaseMySqlService<CooWallet>{
    @Autowired
    private SundayMemberService memberService;
    /**
     *
     * @param request
     * @return
     */
    public PageResult<CooWalletDto> selectWallet(HttpServletRequest request){
        Map<String,Object> param= ParamUtil.packageMysqlPageParam(request,"memberId");
        return new PageResult<CooWalletDto>(packageDto(super.select(param)),super.count(param));

    }

    public List<CooWalletDto> packageDto(List<CooWallet> wallets){
        List<CooWalletDto> result=new ArrayList<CooWalletDto>();
        Map<String,Object> param =new HashMap<String,Object>();
        List<Long> memberIds = new ArrayList<>();
        List<Long> salesIds = new ArrayList<>();
        List<Long> managerIds = new ArrayList<>();
        //匹配用户信息
        for(CooWallet wallet:wallets){
            memberIds.add(wallet.getMemberId());
            salesIds.add(wallet.getSalesId());
            managerIds.add(wallet.getManagerId());
        }

        if(ListUtil.isNotNull(memberIds)){
            param.clear();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("id in",memberIds);
            List<SundayMember> members = memberService.list(param);
            param.clear();
            param.put("id in",salesIds);
            param.put("isDeleted", SundayCommonConstants.NO);
            List<SundayMember> salesMembers=memberService.list(param);
            param.clear();
            param.put("id in",managerIds);
            param.put("isDeleted", SundayCommonConstants.NO);
            List<SundayMember> managerMembers=memberService.list(param);
            for(CooWallet wallet:wallets){
                CooWalletDto dto=new CooWalletDto(wallet.getId(),wallet.getCreateTime(),wallet.getCreateDate(),
                        wallet.getMemberId(),wallet.getMemberLevel(),wallet.getTotalLimit(),
                        wallet.getAbleLimit(),wallet.getTotalAmount(),wallet.getCashAmount(),wallet.getScoreAmount(),wallet.getFrozenAmount(),wallet.getTotalAchievement());
                //配置用户昵称  账号名
                for(SundayMember member:members){
                    if(LongUtil.isNotZreo(wallet.getMemberId())&&
                            wallet.getMemberId().longValue()==member.getId()){
                            dto.setMemberName(member.getNickName());
                            dto.setUserName(member.getUserName());
                        break;
                    }
                }
                //配置推荐人用户昵称  账号名
                if(ListUtil.isNotNull(salesMembers)){
                    for(SundayMember salesMember:salesMembers){
                        if(LongUtil.isNotZreo(wallet.getSalesId())&&
                                wallet.getSalesId().longValue()==salesMember.getId()){
                            dto.setSalesMemberName(salesMember.getNickName());
                            dto.setSalesUserName(salesMember.getUserName());
                            break;
                        }
                    }
                }
                //配置管理人用户昵称  账号名
                if(ListUtil.isNotNull(managerMembers)){
                    for(SundayMember managerMember:managerMembers){
                        if(LongUtil.isNotZreo(wallet.getManagerId())&&
                                wallet.getManagerId().longValue()==managerMember.getId()){
                            dto.setManagerMemberName(managerMember.getNickName());
                            dto.setManagerUserName(managerMember.getUserName());
                            break;
                        }
                    }
                }
                result.add(dto);
            }
        }
        return result;
    }



}
